from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.remote.webdriver import WebDriver

from base.base_page import BasePage
from page.search_page import SearchPage
from page.select_subject_page import SelectSubjectPage
from page.smart_practice_setting_page import SmartPracticeSetPage


class MainPage(BasePage):
    """
    首页
    """
    _moniceyan_element = (AppiumBy.XPATH, "//*[@text='模拟测验")
    _search_frame_element = (AppiumBy.XPATH,
                             "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.RelativeLayout/android.widget.FrameLayout[2]/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View/android.view.View[3]")
    _agree_button = (AppiumBy.XPATH,
                     "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.RelativeLayout/android.widget.FrameLayout[2]/android.webkit.WebView/android.webkit.WebView/android.view.View[6]/android.view.View[2]/android.view.View/android.view.View[4]")

    # 选择学科
    _search_subject = (AppiumBy.XPATH, "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.RelativeLayout/android.widget.FrameLayout[2]/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View/android.view.View[1]")
    # 首页“智能练习”功能按钮
    _smart_practice = (AppiumBy.XPATH,
                       "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.RelativeLayout/android.widget.FrameLayout[2]/android.webkit.WebView/android.webkit.WebView/android.widget.ListView/android.view.View[1]/android.widget.Image")
    # 首页“我的”功能按钮
    _footer_mine_button = (AppiumBy.XPATH,
                           "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.RelativeLayout/android.widget.FrameLayout[1]/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View[4]/android.widget.Image")

    def to_search(self):
        """
        从首页跳转至搜索页
        search页
        """
        # 点击“同意”按钮，进入首页
        # self.driver.find_element(by=AppiumBy.XPATH,
        #                     value="/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.RelativeLayout/android.widget.FrameLayout[2]/android.webkit.WebView/android.webkit.WebView/android.view.View[6]/android.view.View[2]/android.view.View/android.view.View[4]").click()
        # self.find_element_and_click(self._agree_button)
        assert len(self.driver.find_elements(by=AppiumBy.XPATH, value="//*[@text='模拟测验']")) >= 1
        # 点击首页搜搜框
        # self.driver.find_element(by=AppiumBy.XPATH,
        #                          value="/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.RelativeLayout/android.widget.FrameLayout[2]/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View/android.view.View[3]").click()
        self.find_element_and_click(self._search_frame_element)
        return SearchPage(self.driver)

    def to_select_subject(self):
        """
        从首页跳转至学科选择页
        学科选择页
        """
        self.driver.implicitly_wait(3)
        self.find_element_and_click(self._search_subject)
        return SelectSubjectPage(self.driver)

    def to_smart_practice(self):
        """
        从首页跳转至智能练习页
        智能练习页
        """
        self.driver.implicitly_wait(3)
        self.find_element_and_click(self._smart_practice)
        return SmartPracticeSetPage(self.driver)
